package com.dx.financehy.serializer;

import cn.hutool.core.util.IdcardUtil;
import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;

/**
 * 身份证脱敏序列化器
 *@JsonSerialize(using = IdCardHideSerializer.class)
 */
public class IdCardHideSerializer extends JsonSerializer<String> {
    @Override
    public void serialize(String s, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        if (StrUtil.length(s) == 18) {
            jsonGenerator.writeString(IdcardUtil.hide(s, 6, 14));
            return;
        }else if (StrUtil.length(s) == 15){
            jsonGenerator.writeString(IdcardUtil.hide(s, 6, 11));
            return;
        }
        jsonGenerator.writeString(s);
    }
}
